package io.gitee.thinkbungee;

import com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration;
import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import io.gitee.thinkbungee.annotation.condition.ConditionOnComponent;
import io.gitee.thinkbungee.component.push.service.MailPushService;
import io.gitee.thinkbungee.component.push.service.impl.MailPushServiceImpl;
import io.gitee.thinkbungee.constant.SatelliteConstant;
import io.gitee.thinkbungee.crud.mybatis.mapper.injected.SatelliteMybatisSqlInjector;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.text.WordUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 自动装配
 *
 * @author chenyichang
 * @since 2022/1/27
 */
@Configuration
@EnableConfigurationProperties(SatelliteProperties.class)
@Slf4j
public class SatelliteAutoConfiguration {

    /**
     * 组件相关配置
     */
    @Configuration
    public class SatelliteComponentPushAutoConfiguration {
        /**
         * mail组件
         * 当{@code SatelliteConstant.Component.PREFIX + SatelliteConstant.Component.MAIL + SatelliteConstant
         * .Component.ENABLE=true}时mail组件被自动注入（即：spring.satellite.component.mail.enable=true）
         */
        @ConditionOnComponent(SatelliteConstant.Component.MAIL)
        @Configuration
        public class Mail {
            /**
             * mail服务
             *
             * @param mailProperties spring-boot-starter-mail中的配置文件
             * @return mail服务
             */
            @Bean
            public MailPushService mailPushService(MailProperties mailProperties) {
                MailPushServiceImpl mailPushService = new MailPushServiceImpl();
                try {
                    //from的值为：spring.mail.username
                    mailPushService.setFrom(mailProperties.getUsername());
                } catch (Exception e) {
                    throw new IllegalArgumentException("mail服务已生效,请设置 spring.mail.username的值");
                }
                log.info("mail服务:{}已生效", WordUtils.uncapitalize(MailPushService.class.getSimpleName()));
                return mailPushService;
            }
        }

    }

    /**
     * mybatis-plus自动配置
     */
    @Configuration
    @ConditionalOnClass(MybatisPlusAutoConfiguration.class)
    public class SatelliteMybatisPlusAutoConfiguration {

        /**
         * 自定义的一些SQL实现
         *
         */
        @Bean
        public SatelliteMybatisSqlInjector satelliteMybatisSqlInjector() {
            return new SatelliteMybatisSqlInjector();
        }

        /**
         * 乐观锁插件:@Version
         */
        @Bean
        public OptimisticLockerInterceptor optimisticLockerInterceptor() {
            return new OptimisticLockerInterceptor();
        }

        /**
         * 分页插件
         *
         */
        @Bean
        public PaginationInterceptor paginationInterceptor() {
            return new PaginationInterceptor();
        }

    }
}
